package P040_AnonymousClassConstructorIsSpecial;

import org.junit.Test;

/**
 * @ClassName : AnonymousClassConstructorIsSpecial
 * @Description : 匿名类的构造函数很特殊   ---一般类的所有构造函数默认都是调用父类的无参构造，而匿名类没有名字，直接调用父类的同参数构造构造
 * @Author : wukj1
 * @Date: 2022/3/8 17:39
 **/
public class AnonymousClassConstructorIsSpecial {

    @Test
    public void test(){
        Calculator calculator = new Calculator(1, 2) {
            {
                setOperator(Ops.ADD);
            }
        };
        System.out.println(calculator.getResult());

        Add add = new Add(1, 2);
        System.out.println(add.getResult());
    }

}

enum Ops {ADD, SUB}

class Calculator {
    private int i, j, result;

    public Calculator() {

    }

    public Calculator(int _i, int _j) {
        i = _i;
        j = _j;
    }

    protected void setOperator(Ops _op) {
        result = _op.equals(Ops.ADD) ? i + j : i - j;
    }

    public int getResult(){
        return result;
    }
}

class Add extends Calculator{
    {
        setOperator(Ops.ADD);
    }

    public Add(int _i, int _j) {
        super(_i, _j);
    }
}
